#include <openssl/bio.h>

void demo1()
{
    BIO *bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
    BIO_printf(bio_out, "Hello, demo1\n");
    BIO_free(bio_out);
}

void demo2()
{
    BIO *a = BIO_new(BIO_s_file());
    if (a == NULL) {
        printf("[%d]: new failed", __LINE__);
        return;
    }

    if (!BIO_set_fp(a, stdout, BIO_NOCLOSE)) {
        printf("[%d]: set failed", __LINE__);
        BIO_free(a);
        return;
    }

    BIO_printf(a, "Hello, demo2");
    BIO_free(a);
}

void write_to_file_1()
{
    BIO *out = BIO_new_file("filename.txt", "w");
    if (!out) {
        printf("[%d]: new file failed", __LINE__);
        return;
    }

    BIO_printf(out, "Hello, %s", __func__);
    BIO_free(out);
}

void write_to_file_2()
{
    BIO *out = BIO_new(BIO_s_file());
    if (!out) {
        printf("[%d]: new file failed", __LINE__);
        return;
    }

    if (!BIO_write_filename(out, "filename.txt")) {
        printf("[%d]:write failed", __LINE__);
        BIO_free(out);
    }

    BIO_printf(out, "Hello, %s", __func__);
    BIO_free(out);
}

int main(int argc, char *argv[])
{
    demo1();
    demo2();
    write_to_file_1();
    write_to_file_2();

    return 0;
    return 0;
}
